package com.atguigu.gulimall.product.controller;


import com.atguigu.gulimall.product.entity.PmsAttr;
import com.atguigu.gulimall.product.entity.PmsAttrGroup;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.ICategoryService;
import com.atguigu.gulimall.product.service.IPmsAttrGroupService;
import com.atguigu.gulimall.product.service.IPmsAttrService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 属性分组
 *
 * @author szh
 * @email sssss@gmail.com
 * @date 2020-12-21 19:54:05
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
	@Autowired
	private IPmsAttrGroupService attrGroupService;

	@Autowired
	private ICategoryService categoryService;

	@Autowired
	private IPmsAttrService attrService;

	@Autowired
	private AttrAttrgroupRelationService relationService;

	// /product/attrgroup/{attrgroupId}/attr/relation
	@GetMapping("/{attrgroupId}/attr/relation")
	public R listAttrRelation(@PathVariable("attrgroupId") Long attrgroupId ){
		List <PmsAttr> attrEntityList = attrService.getRelationAttr(attrgroupId);
		return R.ok().put("data",attrEntityList);
	}
	// /product/attrgroup/{attrgroupId}/noattr/relation
	@GetMapping("/{attrgroupId}/noattr/relation")
	public R listAttrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
	                            @RequestParam Map<String, Object> params){
		PageUtils page =attrService.getNoRelationAttr(params,attrgroupId);
		return R.ok().put("page", page);
	}
	// /product/attrgroup/attr/relation
	@PostMapping("/attr/relation")
	public R addRelation(@RequestBody List<AttrGroupRelationVO> vos){
		relationService.saveBatch(vos);
		return R.ok();
	}
	/**
	 * 列表
	 */
	@RequestMapping("/list/{catelogId}")
	public R list(@RequestParam Map<String, Object> params,
	              @PathVariable Long catelogId){

		PageUtils page = attrGroupService.queryPage(params,catelogId);

		return R.ok().put("page", page);
	}
	// /product/attrgroup/{catelogId}/withattr ---第17个
	@GetMapping("/{catelogId}/withattr")
	public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
		// 1-查出当前分类下的所有属性分组
		// 2-查出每个属性分组的所有属性
		List<AttrGroupWithAttrsVO> vos =attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
		return  R.ok().put("data",vos);

	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrGroupId}")
	public R info(@PathVariable("attrGroupId") Long attrGroupId){
		PmsAttrGroup attrGroup = attrGroupService.getById(attrGroupId);
		Long catelogId = attrGroup.getCatelogId();
		Long path[] = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
		return R.ok().put("attrGroup", attrGroup);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody PmsAttrGroup attrGroup){
		attrGroupService.save(attrGroup);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody PmsAttrGroup attrGroup){
		attrGroupService.updateById(attrGroup);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

		return R.ok();
	}
	// /product/attrgroup/attr/relation/delete
	@PostMapping("/attr/relation/delete")
	public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos){
		attrService.deleteRelation(vos);
		return R.ok();
	}


}
